package com.chuangjiangx.member.score.web.controller;


import com.chuangjiangx.commons.CurrentThreadContext;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.basic.web.controller.BaseController;
import com.chuangjiangx.member.basic.web.interceptor.Login;
import com.chuangjiangx.member.score.ddd.query.MbrScoreGiftRecordQuery;
import com.chuangjiangx.member.score.ddd.dal.condition.QueryScoreGiftRecordCondition;
import com.chuangjiangx.member.score.ddd.dal.dto.MbrScoreGiftRecordDetail;
import com.chuangjiangx.member.score.ddd.dal.dto.ScoreGiftRecordList;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGiftRecordId;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrUserContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员积分兑换-我的提货码
 */
@RestController
@RequestMapping(value = "/h5/mbr-score-gift-record",produces = "application/json;charset=UTF-8")
public class MbrScoreGiftRecordController extends BaseController {

    private final MbrScoreGiftRecordQuery mbrScoreGiftRecordQuery;

    @Autowired
    public MbrScoreGiftRecordController(MbrScoreGiftRecordQuery mbrScoreGiftRecordQuery) {
        this.mbrScoreGiftRecordQuery = mbrScoreGiftRecordQuery;
    }

    /**
     * 会员积分兑换记录
     * @param claimStatus 兑换状态 0-未取货 1-已领取
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @map data
     */
    @RequestMapping(value = "/record-list")
    @Login
    public Response mbrConvertGiftRecordList(Integer claimStatus) {
        MbrUserContext user = (MbrUserContext) CurrentThreadContext.getCurrentUser();
        QueryScoreGiftRecordCondition condition = new QueryScoreGiftRecordCondition();
        condition.setMerchantId(user.getMerchantId());
        condition.setMobile(user.getMobile());
        condition.setClaimStatus(claimStatus);
        condition.setPageSize(50);
        PagingResult<ScoreGiftRecordList> result = mbrScoreGiftRecordQuery.findMbrScoreGiftRecordList(condition);
        // 过滤取货码为空的
        List<ScoreGiftRecordList> list = result.getItems()
                .stream()
                .filter(e -> StringUtils.isNotBlank(e.getClaimCode()))
                .collect(Collectors.toList());
        return ResponseUtils.successCamel(list);
    }


    /**
     * 会员兑换记录详情
     * @param recordId 记录id
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @see MbrScoreGiftRecordDetail
     */
    @RequestMapping(value = "/record-detail")
    @Login
    public Response mbrConvertGiftRecordDetail(Long recordId) {
        MbrScoreGiftRecordDetail detail = mbrScoreGiftRecordQuery.getMbrScoreGiftRecordDetail(new MbrScoreGiftRecordId(recordId));
        return ResponseUtils.successCamel(detail);
    }
}
